home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / FILES.SWG / 0026_File spliting.pas < prev    next >
Pascal/Delphi Source File  |  1993-08-27  |  3KB  |  127 lines

  1. {
  2. MICHAEL REECE
  3.  
  4. > Hi!  I was wondering.  How would you in Turbo Pascal be able to split a
  5. > single File into two.  I want it to split it to a precise Byte For both
  6. > Files. I want to be able to combine to Files together and split it to its
  7. > original sizes and still be able to work (that no codes are missing etc.).
  8.  
  9. The following is kludgy and only semi tested, but may help you get started.
  10. It's an old little thing I wrote to split large Files to put on a floppy, and
  11. then put it back together again.
  12. }
  13.  
  14. (* usage:  split <Filename> <new-name-for-second-half>
  15.    ex: split nodelist.zip nodelist.zi2
  16. *)
  17. Program Split;
  18.  
  19. Const
  20.   MaxBuffSize = 61140;
  21.  
  22. Type
  23.   BuffType = Array[1..MaxBuffSize] of Byte;
  24.  
  25. Var
  26.   F1, F2   : File;
  27.   Mid      : LongInt;
  28.   Buffer   : ^BuffType;
  29.   BuffSize : LongInt;
  30.   NumRead,
  31.   NumWrite : Word;
  32.  
  33. begin
  34.   Writeln('Splitting File "', ParamStr(1), '"');
  35.   Assign(F1, ParamStr(1));
  36.   Reset(F1, 1);
  37.   Mid:=FileSize(F1) div 2;                     { calculate midpoint }
  38.   Writeln('  Original size: ', FileSize(F1));
  39.   Writeln('  File midpoint: ', Mid);
  40.   Writeln('Creating File "', ParamStr(2), '"');
  41.   Assign(F2, ParamStr(2));
  42.   ReWrite(F2, 1);
  43.   Writeln('Memory available: ', MaxAvail);    { allocate max buffer }
  44.   BuffSize:=MaxAvail;
  45.   if (BuffSize > MaxBuffSize) then
  46.     BuffSize:=MaxBuffSize;
  47.   GetMem(Buffer, BuffSize);
  48.   Writeln('  Buffer size: ', BuffSize);
  49.   Writeln('Seeking to midpoint');
  50.   Seek(F1, Mid);
  51.   Writeln('  Copying remainder of File');
  52.   While (not Eof(F1)) do
  53.   begin
  54.     BlockRead(F1, Buffer^, BuffSize, NumRead);
  55.     BlockWrite(F2, Buffer^, NumRead, NumWrite);
  56.     if (NumRead <> NumWrite) then
  57.     begin
  58.       Writeln('Error in copy');
  59.       Halt(1);
  60.     end;
  61.   end;
  62.   Writeln('Seeking to midpoint');
  63.   Seek(F1, Mid);
  64.   Writeln('  Truncating File');
  65.   Truncate(F1);
  66.   Writeln('Closing Files');
  67.   Close(F2);
  68.   Close(F1);
  69.   Writeln('Done.');
  70. end.
  71.  
  72. { That one splits a File in half. }
  73.  
  74. (* usage:  splice <Filename> <name-of-second-half>
  75.    ex: split nodelist.zip nodelist.zi2
  76.    this will append/splice nodelist.zi1 to nodelist.zip
  77. *)
  78. Program Splice;
  79.  
  80. Const
  81.   MaxBuffSize = 61140;
  82.  
  83. Type
  84.   BuffType = Array[1..MaxBuffSize] of Byte;
  85.  
  86. Var
  87.   F1, F2   : File;
  88.   Buffer   : ^BuffType;
  89.   BuffSize : LongInt;
  90.   NumRead,
  91.   NumWrite : Word;
  92.  
  93. begin
  94.   Writeln('Splicing File "', ParamStr(1), '"');
  95.   Assign(F1, ParamStr(1));
  96.   Reset(F1, 1);
  97.   Writeln('  Original size: ', FileSize(F1));
  98.   Writeln('Appending File "', ParamStr(2), '"');
  99.   Assign(F2, ParamStr(2));
  100.   Reset(F2, 1);
  101.   Writeln('  Original size: ', FileSize(F1));
  102.   Writeln('Memory available: ', MaxAvail);    { allocate max buffer }
  103.   BuffSize:=MaxAvail;
  104.   if (BuffSize > MaxBuffSize) then
  105.     BuffSize:=MaxBuffSize;
  106.   GetMem(Buffer, BuffSize);
  107.   Writeln('  Buffer size: ', BuffSize);
  108.   Writeln('Seeking to end');
  109.   Seek(F1, FileSize(F1));
  110.   Writeln('  Copying File');
  111.   While (not Eof(F2)) do
  112.   begin
  113.     BlockRead(F2, Buffer^, BuffSize, NumRead);
  114.     BlockWrite(F1, Buffer^, NumRead, NumWrite);
  115.     if (NumRead <> NumWrite) then
  116.     begin
  117.       Writeln('Error in copy');
  118.       Halt(1);
  119.     end;
  120.   end;
  121.   Writeln('Closing Files');
  122.   Writeln('Done.');
  123.   Close(F2);
  124.   Close(F1);
  125. end.
  126.  
  127.